package com.yzx.lifeassistants.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.yalantis.contextmenu.lib.ContextMenuDialogFragment;
import com.yalantis.contextmenu.lib.MenuObject;
import com.yalantis.contextmenu.lib.MenuParams;
import com.yalantis.contextmenu.lib.interfaces.OnMenuItemClickListener;
import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.activity.FindActivity;
import com.yzx.lifeassistants.activity.FindSearchActivity;
import com.yzx.lifeassistants.activity.LostActivity;
import com.yzx.lifeassistants.activity.LostSearchActivity;
import com.yzx.lifeassistants.activity.MainActivity;
import com.yzx.lifeassistants.activity.MyFindActivity;
import com.yzx.lifeassistants.activity.MyLostActivity;
import com.yzx.lifeassistants.adapter.CustomPagerAdapter;
import com.yzx.lifeassistants.common.CommonConstant;
import com.yzx.lifeassistants.utils.ToastUtils;

/**
 * @Description: 失物招领
 * @author: yzx
 * @time: 2015-9-15 上午8:49:50
 */

public class LostAndFindFragment extends Fragment implements OnClickListener,
		OnPageChangeListener, OnMenuItemClickListener {
	private RelativeLayout topRL;// 顶部
	private ImageButton topMoreBtn;// 顶部添加按钮
	private ViewPager viewPager;// 中间部分
	private RadioButton lostRB;// 失物找寻
	private RadioButton findRB;// 拾取招领
	private RelativeLayout searchRL;// 搜索
	private CustomPagerAdapter adapter;// viewpage‘s adapter
	public static final int TAB_LOST = 0;// 失物找寻
	public static final int TAB_FIND = 1;// 拾取招领

	private FragmentManager fragmentManager;//
	private ContextMenuDialogFragment mMenuDialogFragment;//
	private int topHeight;// 顶部高度

	/**
	 * @Description: 在onCreateView加载布局
	 */
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_lost_find, container,
				false);
		return view;
	}

	/**
	 * @Description: 在onActivityCreated加载控件
	 */
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initTopView();
		initRadioGroup();
		initSearch();
		initViewPage();
		initMenuFragment();
	}

	/**
	 * @Description: 初始化顶部的标题与按钮
	 */
	private void initTopView() {
		topRL = (RelativeLayout) getView().findViewById(R.id.lost_find_top_rl);
		topMoreBtn = (ImageButton) getView().findViewById(R.id.top_more_btn);
		topMoreBtn.setVisibility(View.VISIBLE);
		topMoreBtn.setOnClickListener(this);
	}

	/**
	 * @Description: 初始化RadioGoup
	 */
	private void initRadioGroup() {
		lostRB = (RadioButton) getView().findViewById(R.id.lost_rb);
		lostRB.setOnClickListener(this);
		findRB = (RadioButton) getView().findViewById(R.id.find_rb);
		findRB.setOnClickListener(this);
	}

	/**
	 * 
	 * @Description: 初始化搜索
	 */
	private void initSearch() {
		searchRL = (RelativeLayout) getView().findViewById(
				R.id.lost_find_search_rl);
		searchRL.setOnClickListener(this);
	}

	/**
	 * @Description: 初始化viewpage
	 */
	@SuppressWarnings("deprecation")
	private void initViewPage() {
		viewPager = (ViewPager) getView()
				.findViewById(R.id.lost_find_viewpager);
		// fragment嵌套viewpage里的多个fragment需要使用getChildFragmentManager
		fragmentManager = getChildFragmentManager();
		List<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(new LostFragment());
		fragments.add(new FindFragment());
		adapter = new CustomPagerAdapter(fragmentManager, fragments);
		viewPager.setAdapter(adapter);
		viewPager.setOffscreenPageLimit(2);
		viewPager.setCurrentItem(TAB_LOST);
		viewPager.setOnPageChangeListener(this);
	}

	/**
	 * 
	 * @Description: 初始化MenuDialogFragment
	 */
	private void initMenuFragment() {
		MenuParams menuParams = new MenuParams();
		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		topRL.measure(w, h);
		topHeight = topRL.getMeasuredHeight();
		menuParams.setActionBarSize(topHeight);
		menuParams.setMenuObjects(getMenuObjects());
		menuParams.setClipToPadding(false);
		menuParams.setClosableOutside(true);
		mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
		mMenuDialogFragment.setItemClickListener(this);
	}

	/**
	 * 
	 * @Description: 设置列表项
	 */
	private List<MenuObject> getMenuObjects() {
		// You can use any [resource, bitmap, drawable, color] as image:
		// item.setResource(...)
		// item.setBitmap(...)
		// item.setDrawable(...)
		// item.setColor(...)
		// You can set image ScaleType:
		// item.setScaleType(ScaleType.FIT_XY)
		// You can use any [resource, drawable, color] as background:
		// item.setBgResource(...)
		// item.setBgDrawable(...)
		// item.setBgColor(...)
		// You can use any [color] as text color:
		// item.setTextColor(...)
		// You can set any [color] as divider color:
		// item.setDividerColor(...)

		List<MenuObject> menuObjects = new ArrayList<MenuObject>();
		int mColor = getActivity().getResources().getColor(
				R.color.actionbar_color);
		// 关闭
		MenuObject close = new MenuObject();
		close.setResource(R.drawable.icon_menu_close);
		close.setBgColor(mColor);
		// 添加失物信息
		MenuObject addLost = new MenuObject("添加失物");
		addLost.setResource(R.drawable.icon_menu_add);
		addLost.setBgColor(mColor);
		addLost.setMenuTextAppearanceStyle(R.style.TextViewStyle);
		// 我的失物信息
		MenuObject myLost = new MenuObject("我的失物");
		myLost.setResource(R.drawable.icon_menu_lost);
		myLost.setBgColor(getActivity().getResources().getColor(
				R.color.actionbar_color));
		myLost.setMenuTextAppearanceStyle(R.style.TextViewStyle);
		// 添加招领信息
		MenuObject addFind = new MenuObject("添加招领");
		addFind.setResource(R.drawable.icon_menu_add);
		addFind.setBgColor(getActivity().getResources().getColor(
				R.color.actionbar_color));
		addFind.setMenuTextAppearanceStyle(R.style.TextViewStyle);
		// 我的招领信息
		MenuObject myFind = new MenuObject("我的招领");
		myFind.setResource(R.drawable.icon_menu_find);
		myFind.setBgColor(getActivity().getResources().getColor(
				R.color.actionbar_color));
		myFind.setMenuTextAppearanceStyle(R.style.TextViewStyle);

		menuObjects.add(close);
		menuObjects.add(addLost);
		menuObjects.add(myLost);
		menuObjects.add(addFind);
		menuObjects.add(myFind);
		return menuObjects;
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int position) {
		switch (position) {
		case TAB_LOST: {// 失物找寻
			// 设置触摸屏幕的模式,这里设置为全屏
			MainActivity.sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
			lostRB.setChecked(true);

			break;
		}
		case TAB_FIND: {// 拾取招领
			// 设置触摸屏幕的模式,这里设置为边缘
			MainActivity.sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
			findRB.setChecked(true);
			break;
		}
		default:
			break;
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lost_rb: {// 失物找寻
			viewPager.setCurrentItem(TAB_LOST);
			break;
		}
		case R.id.find_rb: {// 拾取招领
			viewPager.setCurrentItem(TAB_FIND);
			break;
		}
		case R.id.top_more_btn: {// 更多按钮
			if (fragmentManager
					.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
				mMenuDialogFragment.show(fragmentManager,
						ContextMenuDialogFragment.TAG);
			}
			break;
		}
		case R.id.lost_find_search_rl: {
			Intent intent = new Intent();
			if (viewPager.getCurrentItem() == TAB_LOST) {
				intent.setClass(getActivity(), LostSearchActivity.class);
			} else if (viewPager.getCurrentItem() == TAB_FIND) {
				intent.setClass(getActivity(), FindSearchActivity.class);
			}
			startActivity(intent);
			break;
		}
		default:
			break;
		}

	}

	/**
	 * @Description: 添加返回操作
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == CommonConstant.REQUESTCODE_ADD_LOST_FIND) {
			if (resultCode == CommonConstant.RESULTCODE_NEW_LOST_OK) {
				showToast("失物信息添加成功!");
				viewPager.setCurrentItem(TAB_LOST);
			} else if (resultCode == CommonConstant.RESULTCODE_NEW_FIND_OK) {
				viewPager.setCurrentItem(TAB_FIND);
				showToast("招领信息添加成功!");
			}
		}

	}

	/**
	 * 
	 * @Description: Toast显示信息
	 */
	private void showToast(String message) {
		ToastUtils.showInfoToast(getActivity(), message, R.id.lost_find_top_ll);
	}

	@Override
	public void onMenuItemClick(View clickedView, int position) {
		switch (position) {
		case 0: {// 关闭
			break;
		}
		case 1: {// 添加失物
			Intent intent = new Intent(getActivity(), LostActivity.class);
			intent.putExtra("from", CommonConstant.FROM_LOSTANDFRAGMENT);
			startActivityForResult(intent,
					CommonConstant.REQUESTCODE_ADD_LOST_FIND);
			break;
		}
		case 2: {// 我的失物
			Intent intent = new Intent(getActivity(), MyLostActivity.class);
			startActivity(intent);
			break;
		}
		case 3: {// 添加招领
			Intent intent = new Intent(getActivity(), FindActivity.class);
			intent.putExtra("from", CommonConstant.FROM_ANDFINDFRAGMENT);
			startActivityForResult(intent,
					CommonConstant.REQUESTCODE_ADD_LOST_FIND);
			break;
		}
		case 4: {// 我的招领
			Intent intent = new Intent(getActivity(), MyFindActivity.class);
			startActivity(intent);
			break;
		}
		default:
			break;
		}
	}

}
